//Some of this code from ApolloIM.  Yay GPL
#import "PongPrefsView.h"
#import "PongPrefs.h"
#import "UIKit/UITextView.h"
#import "UIKit/UITransformAnimation.h"
#import "UIKit/UIPreferencesTable.h"
#import "UIKit/UIPreferencesTableCell.h"
#import "UIKit/UIPreferencesTextTableCell.h"
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIView-Rendering.h>
#import "UIKit/UISwitchControl.h"
#import <UIKit/UIWindow.h>
#import <UIKit/CDStructures.h>
#import "Common.h"
@implementation PongPrefsView

extern PongPrefs *sharedPongPrefs;

- (id)initWithFrame:(CGRect)frame
{
    if ((self == [super initWithFrame: frame]) != nil) 
    {
        //LogMethod();
		CGRect rect = frame;
		//NSLog(@"AccountsEditorView>> Init AccountsView...");
		rect.origin.x = rect.origin.y = 0.0f;		
		//Colors
		// CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
		// float whiteComponents[4] = {1, 1, 1, 1};
		// float transparentComponents[4] = {0, 0, 0, 0};
		
		progressIndicator = [[UIProgressIndicator alloc] initWithFrame:frame];
		// [progressIndicator setDisplayedWhenStopped:NO];
		[self addSubview:progressIndicator];
		
        _table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 415.0f)];
        [_table setDataSource:self];
		[_table setDelegate:self];
		[_table reloadData];		
        
		//NSLog(@"AccountEditorView>> Keyboard implementation...");
		NSLog(@"adding _table as subview");
		[self addSubview:	_table];		
		
        //		[_table setKeyboardVisible:NO];
        //		[[_table keyboard]setFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
        
		[self setMode:		 false];
		_delegate = nil;
	}
	return self;
}
//*************************************

- (void)startSpinning
{
	// LogMethod();
	[progressIndicator startAnimation];
}

- (void)stopSpinning
{
	// LogMethod();
	[progressIndicator stopAnimation];
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)aTable 
{
    //LogMethod();
	return 1;
}

- (int)preferencesTable:(UIPreferencesTable *)aTable numberOfRowsInGroup:(int)group 
{
	if (0 == group)                           
		return 2;
	// else
	// 	return 1;
}
- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)aTable cellForGroup:(int)group 
{
    // LogMethod();                            
    UIPreferencesTableCell * cell = [[UIPreferencesTableCell alloc] init];
    return [cell autorelease];
} 

- (UIPreferencesTextTableCell *)preferencesTable:(UIPreferencesTable *)aTable cellForRow:(int)row inGroup:(int)group 
{
    // LogMethod();
    UIPreferencesTextTableCell * cell = [[UIPreferencesTextTableCell alloc] init];
    [cell setEnabled:YES];
    if(group == 0) 
    {
        if(row == 0) 
        {
            [cell setTitle:	@"Host Name"];
            // [cell setPlaceHolderValue:@"127.0.0.1"];					
            [[cell textField]setText: [sharedPongPrefs hostName]];
            [cell setEnabled: YES];
            hostnameField = [cell textField];		
            if(editOrnew)
                [hostnameField setText:[sharedPongPrefs hostName]];	
        } 
        else if(row == 1) 
        {
            [cell setTitle:@"Port"];
            [cell setEnabled:YES];	
            // [cell	setPlaceHolderValue: @"12000"];
			[[cell textField]setText: [NSString stringWithFormat: @"%i",[sharedPongPrefs port]]];
            portField =	[cell textField];
            if(editOrnew)
            {
                NSString *textValOfPort = [NSString stringWithFormat:@"%i", [sharedPongPrefs port]];
                [portField setText:textValOfPort];					
            }
        }
    } 
    // else if(group ==1)
    // {
    //     if(row == 0)
    //     {
    //         [cell setTitle:			@"Activate Networking"];
    //         [cell setEnabled:		          YES];	
    //         [[cell textField]setEnabled:	false];				
    //         if(editOrnew)
    //         {
    //             [networkEnabledSwitch setValue:[[NSNumber numberWithFloat:[networkEnabledSwitch value]] boolValue]];
    //             
    //             if([sharedPongPrefs networkEnabled])
    //             {
    //                 [networkEnabledSwitch setValue:YES];
    //             }
    //             else
    //             {
    //                 [networkEnabledSwitch setValue:NO];
    //             }		
    //         }
    //         [cell addSubview: networkEnabledSwitch];						
    //     }
    // }	
    return [cell autorelease];  
}

- (float)preferencesTable:(UIPreferencesTable *)aTable heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed 
{
    //LogMethod();
    return proposed;
}
- (BOOL)preferencesTable:(UIPreferencesTable *)aTable isLabelGroup:(int)group
{
    //LogMethod();
    return NO;  //totally guessing right now
}

//*************************************		 


- (void)setMode:(bool)_mode
{
	//LogMethod();
	editOrnew = _mode;
}

- (bool)getMode
{
	return editOrnew;
}

-(void)updatePrefs
{
	// LogMethod();
	[sharedPongPrefs setHostName:[hostnameField text]];
	NSScanner *scanner = [NSScanner scannerWithString:[portField text]];
	int port;
	while (NO == [scanner isAtEnd])
	{
		[scanner scanInt:&port];
	}
    
	[sharedPongPrefs setPort:port];
	// if([[NSNumber numberWithFloat:[networkEnabledSwitch value]]boolValue])
	// {
	// 	[sharedPongPrefs setNetworkEnabled:YES];
	// }
	// else
	// {		
	// 	[sharedPongPrefs setNetworkEnabled:NO];
	// }
}

- (void)dealloc
{
	// LogMethod();
	[_table release]; _table = nil;
	[progressIndicator release]; progressIndicator = nil;
	[super dealloc];
}

- (void)setDelegate:(id)delegate
{
	// LogMethod();
	_delegate = delegate;
}
@end
